package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.AdChannel;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    public WmChannelMapper wmChannelMapper;

    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        WmChannel wmChannel = wmChannelMapper.selectById(adChannel.getId());
        BeanUtils.copyProperties(adChannel,wmChannel);
        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult("修改成功");
    }

    /**
     * 后台端删除频道
     */
    @Override
    public ResponseResult delAdChannel(Integer id) {
        if (id == null || id < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getId, id);
        wmChannelMapper.delete(query);
        return ResponseResult.okResult("删除成功");
    }

    /**
     * 后台端新增频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveAdChannel(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);

        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult("保存成功");

    }

    /**
     * 带条件分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageList(ChannelDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //创建分页条件
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //如果带搜索词,创建模糊查询的条件
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            query.like(WmChannel::getName, dto.getName());

        }

        //查询分页信息
        IPage<WmChannel> iPage = wmChannelMapper.selectPage(page, query);
        //创建分页结果,分装
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        System.out.println(iPage.getRecords());
        //返回结果
        return pageResponseResult;
    }

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        List<WmChannel> wmChannels = wmChannelMapper.selectList(Wrappers.emptyWrapper());

        return ResponseResult.okResult(wmChannels);
    }
}